<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Member\Good;
use App\Models\Member\Grade;
use App\Models\Member\GoodPrice;
use App\Models\Member\User;
use App\Models\Member\DealRecord;
use App\Common\AliossHandler;
use Auth, Input, Log;
class StoreController extends Controller
{
    // 获取购物车内容
    public function getUserCart(Request $request)
    {
        $user = Auth::user()->toArray();
        $storeId = $user['store_id'];
        $data = $request->json('data');
        if (empty($data)) {
            return $this->errorClient([], '购物车数据为空');
        }
        $info = json_decode($data, true);
        $jf = 0;// 历史积分 - 决定了 是每次购买都重头开始计算 还是 看历史数据 , 目前只按照每次购买来算
        // 获取 上线的会员产品
        $goodsArray=array_keys($info);
        $Good = new Good;
        $goodsData = $Good->storeGoods($storeId, $goodsArray);
        $dataCart = $Good->foreachCart($info, $goodsData);
        $retailPrice = round($dataCart['retailPrice']/100, 2);
        $pointsAll = round($dataCart['pointsAll'], 2);
        $goodsNewArray = $dataCart['goodsNewArray'];
        
        $Grade = new Grade;
        $grades = $Grade->storeGrade($storeId);// 获取 当前店铺的会员等级信息
        $gradesKeyVal = key_val($grades,'level');
        $GoodPrice = new GoodPrice;
        $priceArr = $GoodPrice->storeGoodsPrice($storeId, $goodsNewArray, $gradesKeyVal);
        
        
        $jf = $jf + $retailPrice;
        $tips = $Grade->getTips($grades, $jf, $user);// 获取 底部提示文字 获取用户当前使用的等级价格 距离下个等级还差多少; 
        
        Log::info('购物车积分:'.$jf);
        Log::info('购物车等级:'.$tips['priceGrade']);
       
        $endData = $this->foreachGoods($goodsData, $priceArr, $info, $tips, $retailPrice, $pointsAll);// 遍历商品数据
        return $this->successClient($endData, '获取购物车数据成功');
    }
    // 遍历商品数据 获取最终购物车返回数据 [ 商品数据 / 价格等级数据 / 购物车数据 / 底部信息 / 总零售价 / 应得积分]
    private function foreachGoods($goodsData, $priceArr, $info, $tips, $retailPrice, $pointsAll)
    {
        $endData=[];
        $endData['retail_price'] = $retailPrice;
        $endData['points_all'] = $pointsAll;
        $endData['pay_price_all'] = 0;
        // $endData['tips_text'] = $tips['tipsText'];
        // $endData['price_grade'] = $tips['priceGrade'];
        $endData['price_grade'] = 0;//现取消多等级价格 获取会员价格信息
        foreach ($goodsData as $keyGoods => $valueGoods) {
            $payPrice = $priceArr[$valueGoods['id']][$endData['price_grade']]['price'];
            $num = $info[$valueGoods['id']]['num'];

            if ($info[$valueGoods['id']]['pitch'] == true) {
                $endData['pay_price_all'] += $payPrice*$num;
            }
            
            $endData['goodscard'][$valueGoods['id']]['pitch'] = $info[$valueGoods['id']]['pitch'];
            $endData['goodscard'][$valueGoods['id']]['id'] = $valueGoods['id'];
            $endData['goodscard'][$valueGoods['id']]['cover'] = AliossHandler::read('good', $valueGoods['cover']);
            $endData['goodscard'][$valueGoods['id']]['title'] = $valueGoods['title'];
            $endData['goodscard'][$valueGoods['id']]['sku'] = $valueGoods['sku'];
            $endData['goodscard'][$valueGoods['id']]['base_price'] = $valueGoods['base_price']/100;
            $endData['goodscard'][$valueGoods['id']]['pay_price'] = $payPrice;
            $endData['goodscard'][$valueGoods['id']]['num'] = $num;
            $endData['goodscard'][$valueGoods['id']]['good_type_id'] = $valueGoods['good_type_id'];
            $endData['goodscard'][$valueGoods['id']]['price_arr'] = $priceArr[$valueGoods['id']];
        }
        return $endData;
    }
}
